package polimi.disttest;

import polimi.reds.Message;

/**
 * This class represent a generic message
 * 
 * @author daniel
 *
 */
public class GenericMessage extends Message {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5060900661571950877L;
	private Integer messageClass;
	private Integer seqNumber;
	
	/**
	 * Create a message with the specified id
	 * 
	 * @param id Identifier of the message
	 */
	public GenericMessage(Integer id, Integer seqNumber) {
		this.messageClass = id;
		this.setSeqNumber(seqNumber);
	}
	
	public GenericMessage(Integer id) {
		this.messageClass = id;
		this.setSeqNumber(0);
	}
	
	public boolean equals(Object obj) {
		if (!(obj instanceof GenericMessage)) {
			return super.equals(obj);
		}
		
		return messageClass.equals(((GenericMessage) obj).getMessageClass());
	}
	
	/**
	 * Get the id of the message
	 * @return the id of the message
	 */
	public Integer getMessageClass() {
		return messageClass;
	}
	
	/**
	 * Convert the message to a string
	 * @return the message converted to string
	 */
	public String toString() {
		if (seqNumber>0)
			return messageClass + "(seq:" + seqNumber + ")";
		else
			return "" + messageClass;
	}
	
	public int hashCode() {
		return ("" + messageClass).hashCode();
	}

	public void setSeqNumber(Integer seqNumber) {
		this.seqNumber = seqNumber;
	}

	public Integer getSeqNumber() {
		return seqNumber;
	}
}
